package cn.changein.framework.modular.fileInfo.controller;

import cn.changein.framework.kernel.model.response.ResponseData;
import cn.changein.framework.modular.fileInfo.service.FileService;
import cn.changein.framework.modular.templateInfo.model.params.TemplateInfoParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RequestMapping("/file")
@Controller
public class FileController {

    @Autowired
    private FileService fileService;
    Logger log = LoggerFactory.getLogger(FileController.class);

    /**
     * 跳转到主页面
     *

     * @Date 2020-10-28
     */
    @RequestMapping("/fileManager")
    public String index() {
        return "/modular/fileManager/filaManager.html";
    }


    /*

     * @Description //TODO 文件上传
     * @Date 2020-11-27 09:32
     * @MethodName uploadPicture
     * @Param [file, moduleCode, moduleName, servletRequest, httpServletResponse]
     * @return cn.changein.framework.kernel.model.response.ResponseData
     **/
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData uploadPicture(@RequestParam(value = "file",required = false) MultipartFile file,@RequestParam(value = "upfile",required = false) MultipartFile upfile, @RequestParam("moduleCode") String moduleCode, @RequestParam("moduleName") String moduleName, HttpServletRequest servletRequest, HttpServletResponse httpServletResponse)
            throws IOException {
        file = file == null ? upfile :file;
        return fileService.uploadFile(file, moduleCode,moduleName, servletRequest,httpServletResponse);
    }

    /*

     * @Description //TODO 导入模板文件上传
     * @Date 2020-11-27 09:32
     * @MethodName uploadPicture
     * @Param [file, moduleCode, moduleName, servletRequest, httpServletResponse]
     * @return cn.changein.framework.kernel.model.response.ResponseData
     **/
    @RequestMapping(value = "/templateUploadFile", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData templateUploadFile(@RequestParam("file") MultipartFile file,TemplateInfoParam templateInfoParam, HttpServletRequest servletRequest, HttpServletResponse httpServletResponse)
            throws IOException {
        ResponseData responseData = fileService.templateUploadFile(file, templateInfoParam, servletRequest, httpServletResponse);
        return responseData;
    }
    /*

     * @Description //TODO 文件下载
     * @Date 2020-11-27 09:32
     * @MethodName downLoadFile
     * @Param [moduleCode, moduleName, fileName, servletRequest, httpServletResponse]
     * @return cn.changein.framework.kernel.model.response.ResponseData
     **/
    @RequestMapping(value = "/downLoadFile", method = RequestMethod.POST)
    @ResponseBody
    public void downLoadFile(@RequestParam("moduleCode") String moduleCode,@RequestParam("moduleName") String moduleName,@RequestParam("fileName") String fileName,HttpServletRequest servletRequest,  HttpServletResponse httpServletResponse)
            throws IOException {
        fileService.downLoadFile( moduleCode,moduleName,fileName,servletRequest,httpServletResponse);
    }


    /*

     * @Description //TODO 导出
     * @Date 2020-11-27 09:32
     * @MethodName downLoadFile
     * @Param [moduleCode, moduleName, fileName, servletRequest, httpServletResponse]
     * @return cn.changein.framework.kernel.model.response.ResponseData
     **/
    @RequestMapping(value = "/exportData", method = RequestMethod.POST)
    @ResponseBody
    public void exportData(@RequestParam("moduleCode") String moduleCode,@RequestParam("moduleName") String moduleName,@RequestParam("fileName") String fileName,HttpServletRequest servletRequest,  HttpServletResponse httpServletResponse)
            throws IOException {
        fileService.downLoadFile( moduleCode,moduleName,fileName,servletRequest,httpServletResponse);
    }


    /**
     * 在线预览文件
     * @param finalName  文件唯一标识id
     * @param request
     * @param response
     */
    @RequestMapping(value = "/previewPdf", method = RequestMethod.GET)
    @ResponseBody
    public void  previewPdf(@RequestParam(required = true) String finalName,HttpServletRequest request,HttpServletResponse response){
       this.fileService.previewPdfFile(finalName,request,response);
    }



}